﻿Imports System.Text.RegularExpressions
'============================================

'Author: Oliver Degnan
'Namespace Level: Page

'============================================
Namespace DotNetNukeIta.Modules.RSSNuke.Utils

    Public Class StringFunctions

        Public Shared Function StringCount(ByVal strText As String) As Integer
            'return the length of the string
            Return strText.Length
        End Function

        Public Shared Function RemoveHTMLTags(ByVal strText As String) As String
            'remove some closing and some opening HTML tags
            Dim regularExpressionString As String = _
          "</[c-g\d]+>|" & _
          "</[i-o\d]+>|" & _
          "</[a\d]+>|" & _
          "</[q-z\d]+>|" & _
          "<[cg]+[^>]*>|" & _
          "<[i-o]+[^>]*>|" & _
          "<[q-z]+[^>]*>|" & _
          "<[a]+[^>]*>|" & _
          "<[p]+[^>]*>|" & _
          "<[br]+[^>]*>"

            Dim r As New Regex(regularExpressionString)
            Return r.Replace(strText, " ")
        End Function

        Public Shared Function FormatText(ByVal strText As String, ByVal minLength As Integer, ByVal maxLength As Integer) As String
            'format input text
            'string length must be greater than minimum length specified
            If StringCount(strText) > minLength Then
                'AND string length must be less OR equal to the maximum length specified
                If StringCount(strText) <= maxLength Then
                    'remove all unwanted HTML tags
                    Return RemoveHTMLTags(strText)
                Else
                    'cut string at maximum length allowed
                    Return Left(RemoveHTMLTags(strText), maxLength)
                End If
            Else
                'return an error message
                Return "A minimum text length of " & minLength & " characters is required."
            End If
        End Function

    End Class

End Namespace